package goods

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"weekly-exam-1/config"
	"weekly-exam-1/internal/model/dao"
)

type Add struct {
	Bank     int     `form:"bank" json:"bank" xml:"bank"  `
	BigSmall int     `form:"bigSmall" json:"bigSmall" xml:"bigSmall"`
	BuyType  int     `form:"buyType" json:"buyType" xml:"buyType" `
	Looks    int     `form:"looks" json:"looks" xml:"looks"`
	Price    float64 `form:"price" json:"price" xml:"price" binding:"required"`
}

func GOOdAdd(c *gin.Context) {
	var json Add
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	value, _ := c.Get("userId")
	u := value.(uint)
	goods := dao.Goods{
		UserId:   int(u),
		Bank:     int8(json.Bank),
		BigSmall: int8(json.BigSmall),
		BuyType:  int8(json.BuyType),
		Looks:    int8(json.Looks),
		Price:    json.Price,
	}
	config.DB.Create(&goods)
	c.JSON(http.StatusOK, gin.H{
		"msg":  "发布成功",
		"data": goods.ID,
	})
}
